'''
The module is a central management to install overriding modules
and (in the future) any setup needed for the simulation process.

@author: Hatem Oraby
'''

import simConfig
import time
import override._time
import socket
import override._socket
import select
import override._select
import override._asyncore
from overrideManager import OverrideManager

def installTime():
    '''Overrides C{time} module '''
    #if not simConfig.Configuration.timeInstalled:
    from sys import modules
    if "__timeInstalled" not in modules:
        simConfig.Configuration.timeInstalled = True
        override._time.installModule()
        modules["__timeInstalled"] = True 

def installSocket():
    '''Overrides C{socket} and C{select} modules '''
    #if not simConfig.Configuration.socketInstalled:
    from sys import modules
    if "__socketsInstalled" not in modules:
        simConfig.Configuration.socketInstalled = True
        override._socket.installModule()
        override._select.installModule()
        installFcntl()
        modules["__socketsInstalled"] = True

def installReactorBase():
    '''
    Installs the overriding methods in for U{twisted.internet.base
    <http://twistedmatrix.com>}
    '''
    from sys import modules
    if "__reactorBaseInstalled" not in modules:
        from twistedSim.override.twisted.python import _runtime
        _runtime.installAll()
        from twistedSim.override.twisted.internet.base import InstallSignalReactor
        InstallSignalReactor()
        #simConfig.Configuration.reactorInstalled = True
        modules["__reactorBaseInstalled"] = True

def installPosixBase():
    '''
    Installs the overriding methods in for U{twisted.internet.posixbase
    <http://twistedmatrix.com>}
    '''
    from sys import modules
    if "__reactorPosixInstalled" not in modules:
        from twistedSim.override.twisted.internet import posixbase
        posixbase.install()
        modules["__reactorPosixInstalled"] = True

def installReactorSelect():
    '''
    Installs U{twisted.internet.select <http://twistedmatrix.com>}
    
    In fact this method just calls L{installReactorBase},
    as there are no special methods overridden especially for 
    select reactor.
    '''
    installReactorBase()
    installPosixBase()
    #simConfig.Configuration.reactorInstalled = True

def installAsyncore():
    '''Experimental overriding for Python's Asyncore Module 
    '''
    override._asyncore.install()

def uninstallAsyncore():
    #override._asyncore.uninstall()
    pass

def installFcntl():
    '''I do not override, I rather do a bad hack '''
    from sys import modules
    if "__fnctlInstalled" not in modules:
        try:
            import fcntl
        except ImportError:
            pass
        else:
            #if fcntl and hasattr(fcntl, "FD_CLOEXEC"):
            #    del fcntl.FD_CLOEXEC
            if fcntl: #bad hack for twisted 10
                def dummy(*args,**kargs):
                    return 1
                fcntl.fcntl = dummy
        modules["__fnctlInstalled"] = True       

def installAll():
    '''Install all the modules '''
    installTime() #Should i force Simulation time here??
    installSocket()
    #installReactorBase()
    installReactorSelect()
    installAsyncore()
